import requests
import logging
import json
import yaml
import pytest

def readjson(): #定义了一个函数，读取json文件内容
    with open("user.json", "r", encoding="utf8") as f:
        data = json.load(f)
        return data

def readdailidi_json(): #定义了一个函数，读取json文件内容
    with open("dailidi.json", "r", encoding="utf-8") as f:
        data = json.load(f)
        return data
def readyaml():#定义了一个函数，读取yaml文件内容
    with open("userymal.yaml", "r", encoding="utf8") as f:
        data = yaml.safe_load(f)
        return data

class Testhelp():
    token=""
    def setup_method(self):
        print("初始化环境")
        print("用例开始执行")
    def teardown_method(self):
        print("清理环境")
        print("用例结束执行")

    # case001 登录成功
    @pytest.mark.parametrize("data",readyaml())
    def test_login(self,data):
        try:
            url = "http://10.59.9.21/api/user/login"
            data={"username":data.get("username"),"password":data.get("password")}
            r = requests.post(url=url,json=data)
            logging.info(r.json())
            print("登录：",r.json())
            Testhelp.token=r.json()['data']['token']
        except Exception as e:
            logging.info("错误信息：",e)
        assert "登录成功" in r.text
        assert r.json()["codeMsg"] == 'SUCCESS OK'
        assert r.status_code==200

    # case002 登录密码为空，返回密码不能为空
    # def test_login_mima_kong(self):
    #     try:
    #         url = "http://10.59.9.21/api/user/login"
    #         data = {"username": "admin", "password": ""} #密码空
    #         r = requests.post(url=url, json=data)
    #         logging.info(r.json())
    #         print("登录：", r.json())
    #     except Exception as e:
    #         logging.info("错误信息：", e)
    #     assert "密码不能为空" in r.text
    #     assert r.json()["codeMsg"] == 'NOT NULL VALUE'
    #     assert r.status_code==200


    #新增代理地址
    @pytest.mark.parametrize("dizhi",readdailidi_json())
    def test_addaddr(self,dizhi):
        url = "http://10.59.9.21/api/user/register"
        data = {"username":dizhi.get("username"),"password":"+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
                "checkPwd":"+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=","a_id":dizhi.get("a_id"),
                "dtype":2,"phone":dizhi.get("phone"),"deadline":"2025-12-30"}
        tou={"token":Testhelp.token}
        r = requests.post(url=url, json=data,headers=tou)
        logging.info(r.json())
        print("新增代理地址：", r.json())

